﻿package com.util.asyncPost;

import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

class AsyncClient {
	private static final int TIME_OUT = 6000;
	private static DefaultHttpClient client;

	private AsyncClient() {

	}

	public static DefaultHttpClient getHttpClient() {
		if (client == null)
			createNewClient();
		return client;
	}

	private static void createNewClient() {
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, TIME_OUT);
		HttpConnectionParams.setSoTimeout(params, TIME_OUT);
		SchemeRegistry schreg = new SchemeRegistry();
		schreg.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		schreg.register(new Scheme("https", PlainSocketFactory
				.getSocketFactory(), 443));
		ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(
				params, schreg);

		client = new DefaultHttpClient(connManager, params);
	}
}
